<?php

/*
	Class: Watch_Email

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Watch_Email {

	/* Group: Instance Variables */

	/*
		Variable: $_template_path
	*/
	var $_template_path = null;

	/* Group: Constructors */

	/*
		Constructor: Watch_Email
			Sets up a template path variable with which to override the one in the Cts_Email Class.
	*/
	function Watch_Email() {
        $theme_locations = Zend_Registry::get('theme_locations');
        $theme_path = $theme_locations['frontend']['current_theme']['path'];
		$this->_template_path = $theme_path.Cts_Registry::get('email_template_path');
	}

	/* Group: Instance Methods */

	/*
		Function: sendNotification
			Sends an email notifying a user that they have a new feed item.
			The username in the feed item is used to find the email address to use.

		Arguments:
			feed_item_id - The ID of the feed item about which to notify the recipient via email.

		Returns: void
	*/
	function sendNotification($feed_item_id) {
		// get the feed item
		$feed_items_table = new WatchFeedItems();
		$feed_item = $feed_items_table->getItemById($feed_item_id);
		// find the name and email address of the subscriber
		$username = $feed_item['username'];
		$users_table = new Users();
		$user = $users_table->fetchByUsername($username)->toArray();
		$to_address = $user->email;
		$to_name = $user->fullname;
		// set up the subject
		$subject_params = array();
		$subject = Cts_Registry::get('default_email_alert_subject_format');
		// set up the body
		$template = "notification.tpl";
		$template_params = array(
			'author' => $feed_item['author'],
			'title' => $feed_item['title'],
			'description' => $feed_item['description'],
			'url' => $feed_item['url'],
			);
		// send the email
		$emailer = new Cts_Email($this->_template_path);
		$emailer->sendHtmlEmail($subject, $to_address, $template, $template_params, $to_name);
	}

	/*
		Function: sendDigest

		Arguments:
			username - The username of the subscriber to get feed items for.
			period - The length of time, in seconds (a Unix timestamp), to go back and get feed items from.

		Returns: void
	*/
	function sendDigest($username, $period) {
		// find the name and email address of the subscriber
		$users_table = new Users();
		$user = $users_table->fetchByUsername($username)->toArray();
		$to_address = $user->email;
		$to_name = $user->fullname;
		// find the earliest date to get by subtracting the timestamp from the current timestamp and converting it to a date
		$go_back_to = date("Y/m/d", time() - $period);
		$now = date("Y/m/d", time());
		// get the feed items between now and the date in the past
		$feed_items_table = new WatchFeedItems();
		$feed_items = $feed_items_table->getItemsByDateRange($username, $go_back_to, $now);
		// set up the subject
		$subject_params = array();
		$subject = Cts_Registry::get('default_email_digest_subject_format');
		// set up the body
		$template = "digest.tpl";
		$template_params = array(
			'feed_items' => $feed_items,
			'period_name' => "weekly", // TODO make this dynamic
		);
		// send the email
		$emailer = new Cts_Email($this->_template_path);
		$emailer->sendHtmlEmail($subject, $to_address, $template, $template_params, $to_name);
	}

}
